{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pprint import pprint\n",
    "from osgeo import gdal, ogr, osr\n",
    "\n",
    "import shlex, subprocess\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "shp_fn = 'contour_line.shp'\n",
    "new_raster_fn = 'out.tif'\n",
    "fill_raster_fn = 'source.tif'\n",
    "ts = '1285 1772'\n",
    "\n",
    "datasource = ogr.Open(shp_fn)\n",
    "layer = datasource.GetLayer()\n",
    "min_x, max_x, min_y, max_y = layer.GetExtent()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['gdal_rasterize',\n",
      " '-l',\n",
      " 'contour_line',\n",
      " '-a',\n",
      " 'Elevation',\n",
      " '-ts',\n",
      " '1285',\n",
      " '1772',\n",
      " '-te',\n",
      " '105.84905940540116',\n",
      " '40.09169938545413',\n",
      " '106.2900121828004',\n",
      " '40.69988238134727',\n",
      " '-a_nodata',\n",
      " '0.0',\n",
      " 'contour_line.shp',\n",
      " 'out.tif']\n"
     ]
    }
   ],
   "source": [
    "cmd_rasterize = f\"gdal_rasterize -l {layer.GetName()} -a Elevation -ts {ts} \" +\\\n",
    "      f\"-te {min_x} {min_y} {max_x} {max_y} -a_nodata 0.0 {shp_fn} {new_raster_fn}\"\n",
    "\n",
    "args_rasterize = shlex.split(cmd_rasterize)\n",
    "pprint(args_rasterize)\n",
    "\n",
    "# result = subprocess.run(cmd, stdout=subprocess.PIPE)\n",
    "# stdout = result.stdout.decode('utf-8')\n",
    "# print(stdout)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "succeed!\n"
     ]
    }
   ],
   "source": [
    "result = subprocess.run(args_rasterize, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
    "stderr = result.stderr.decode('utf-8')\n",
    "if stderr == '':\n",
    "    print(\"succeed!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "max_distance = 24\n",
    "band = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['gdal_fillnodata.py',\n",
      " '-md',\n",
      " '24',\n",
      " '-b',\n",
      " '1',\n",
      " '-of',\n",
      " 'GTiff',\n",
      " 'out.tif',\n",
      " 'source.tif']\n"
     ]
    }
   ],
   "source": [
    "cmd_fill = f\"gdal_fillnodata.py -md {max_distance} -b {band} -of GTiff \" +\\\n",
    "           f\"{new_raster_fn} {fill_raster_fn}\"\n",
    "args_fill = shlex.split(cmd_fill)\n",
    "\n",
    "pprint(args_fill)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = subprocess.run(args_fill, stdout=subprocess.PIPE, etderr=subprocess.PIPE)\n",
    "stderr = result.stderr.decode('utf-8')\n",
    "\n",
    "if stderr == '':\n",
    "    print(\"succeed!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys, tempfile, uuid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/lane/Code/Work/Toolbox/contour2dem'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'/tmp/07e48fd5-40f5-4c9d-a51b-16023a73cba4.tif'\n"
     ]
    }
   ],
   "source": [
    "filename = f\"{uuid.uuid4()}.tif\"\n",
    "# tmp_fn = tempfile.gettempdir()\n",
    "filename = os.path.join(tempfile.gettempdir(), filename)\n",
    "\n",
    "pprint(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/lane/Code/Work/Toolbox/contour2dem/contour_line.shp'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.path.join(os.getcwd(), 'contour_line.shp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
